package com.sssre.lttcloud.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sssre.lttcloud.api.entity.DictData;
import com.sssre.lttcloud.common.result.Result;
import com.sssre.lttcloud.common.result.ResultCode;
import com.sssre.lttcloud.dict.mapper.DictDataMapper;
import com.sssre.lttcloud.dict.service.DictDataService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 字典数据服务实现类
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Override
    public Result getDictDataPage(Integer pageNum, Integer pageSize, String dictType, String dictLabel) {
        try {
            Page<DictData> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
            
            if (StringUtils.hasText(dictType)) {
                queryWrapper.eq(DictData::getDictType, dictType);
            }
            if (StringUtils.hasText(dictLabel)) {
                queryWrapper.like(DictData::getDictLabel, dictLabel);
            }
            
            queryWrapper.orderByAsc(DictData::getSortOrder);
            
            IPage<DictData> result = this.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<List<DictData>> getDictDataByType(String dictType) {
        try {
            LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictData::getDictType, dictType)
                       .eq(DictData::getStatus, 1)
                       .orderByAsc(DictData::getSortOrder);
            
            List<DictData> list = this.list(queryWrapper);
            return Result.success(list);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<DictData> getDictDataById(Long id) {
        try {
            DictData dictData = this.getById(id);
            if (dictData == null) {
                return Result.failed(ResultCode.DATA_NOT_FOUND);
            }
            return Result.success(dictData);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> addDictData(DictData dictData) {
        try {
            boolean success = this.save(dictData);
            return success ? Result.success("添加成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> updateDictData(DictData dictData) {
        try {
            boolean success = this.updateById(dictData);
            return success ? Result.success("修改成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> deleteDictData(Long id) {
        try {
            boolean success = this.removeById(id);
            return success ? Result.success("删除成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> deleteDictDatas(List<Long> ids) {
        try {
            boolean success = this.removeByIds(ids);
            return success ? Result.success("批量删除成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> getDictLabel(String dictType, String dictValue) {
        try {
            LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictData::getDictType, dictType)
                       .eq(DictData::getDictValue, dictValue)
                       .eq(DictData::getStatus, 1);
            
            DictData dictData = this.getOne(queryWrapper);
            if (dictData == null) {
                return Result.failed(ResultCode.DATA_NOT_FOUND);
            }
            return Result.success(dictData.getDictLabel());
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> getDictValue(String dictType, String dictLabel) {
        try {
            LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictData::getDictType, dictType)
                       .eq(DictData::getDictLabel, dictLabel)
                       .eq(DictData::getStatus, 1);
            
            DictData dictData = this.getOne(queryWrapper);
            if (dictData == null) {
                return Result.failed(ResultCode.DATA_NOT_FOUND);
            }
            return Result.success(dictData.getDictValue());
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }
} 